#include"Coin.h"
#define coin_speed 0.015f
CCoin::CCoin(int _kind,int _x, int _y, int _width, int _height)
{
	kind=_kind;
	x=_x;
	y=_y;
	sy=y;
	vy=0;
	last_time=0;
	width=_width;
	height=_height;
	
	isover=false;
	if(kind==31)
	{
		coin=new CSprite(false,0,128,25,25,0,2,0);
		isshow=true;
		isactive=false;
	}
	else // 22
	{
		coin=new CSprite(false,0,152,25,25,0,3,0);
		isshow=true;
		isactive=true;
	}
}
CCoin::~CCoin()
{
	if(coin!=NULL) delete coin;
}
void CCoin::CollisionWithStaticObject(CStaticObjects *StaticObject,RECT screen)
{
	RECT coinRect=coin->getRECT(x,y);
	RECT objectRect;
	int kq=0;
	StaticObject->tree->Collision(StaticObject->tree->root,screen,coinRect,&kq,&objectRect);
	if(kq!=0)
	{
		isactive=true;
	}
}
int CCoin::CollisionWithMario(RECT marioRect)
{
	if((kind==31&&isover==true)||(kind==22)) return 0;
	RECT coinRect=coin->getRECT(x,y);
	RECT dest;
	if(IntersectRect(&dest,&coinRect,&marioRect))
	{
		if(kind==31) isover=true;
		
		return 1;
	}
	else return 0;
}
void CCoin::Render(LPD3DXSPRITE sprite, LPDIRECT3DTEXTURE9 image, int time, int xcamara)
{
	if(isover==false)
	{
		if(isactive==true)
		{
			vy=-coin_speed;
		}
		y+=vy*time*time;
		if(sy-y>150) 
		{
			isactive=false;
			isover=true;
		}
		if(isshow==true)
		{
			DWORD now=GetTickCount();
			if(now-last_time>30)
			{
				coin->Next();
				last_time=now;
			
			coin->Render(sprite,image,x-xcamara,y);
			}
		}
	}
}